package com.vitabuybuy.tv.ui.mine.coin.viewmodel

import android.view.View
import androidx.databinding.ObservableArrayList
import androidx.lifecycle.MutableLiveData
import com.library.common.base.BaseViewModel
import com.library.common.base.MessageModel
import com.library.common.ext.rxHttpRequest
import com.library.common.net.error.msg
import com.library.common.net.loadingtype.LoadingType
import com.library.common.toast.XToast
import com.vitabuybuy.tv.BR
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.dto.CoinRecordDto
import com.vitabuybuy.tv.dto.RechargeDto
import com.vitabuybuy.tv.network.BiddingRepository
import com.vitabuybuy.tv.network.CoinRepository
import me.tatarka.bindingcollectionadapter2.ItemBinding

class RewardCoinViewModel : BaseViewModel() {

    val recordlist = ObservableArrayList<CoinRecordDto>()
    val recordBinding = ItemBinding.of<CoinRecordDto>(BR.itemBean, R.layout.item_use_record)

    fun getRecordList(pageNumber: Int) {
        rxHttpRequest {
            onRequest = {
                val it = CoinRepository.getCoinRecord(pageNumber, 10).await()
                if (pageNumber == 1) recordlist.clear()
                recordlist.addAll(it)
                if (recordlist.isEmpty()) {
                    statusView.set(LoadingType.SHOW_EMPTY)
                }else{
                    statusView.set(LoadingType.SHOW_CONTENT)
                }
                defUI.msgEvent.postValue(MessageModel(1, arg1 = it.size))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isLoadingType = true
            isRefresh = pageNumber == 1
        }
    }

    fun getMyCoin() {
        rxHttpRequest {
            onRequest = {
                val it = CoinRepository.getMyCoin().await()
                defUI.msgEvent.postValue(MessageModel(2, arg1 = it))
            }
            onError = {
                XToast.normal(it.msg)
            }
        }
    }

    val rechargelist = MutableLiveData<MutableList<RechargeDto>>()

    fun getRechargeList() {
        rxHttpRequest {
            onRequest = {
                rechargelist.value = CoinRepository.getRechargeList().await()
            }
            onError = {
                XToast.normal(it.msg)
            }
        }
    }

    fun rechargeCoin(id: Int, goldQuantity: Int, paymentMethod: String) {
        rxHttpRequest {
            onRequest = {
                CoinRepository.rechargeCoin(id, paymentMethod).await()
                defUI.msgEvent.postValue(MessageModel(1, arg1 = goldQuantity))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }
}
